<template>
  <div class="mod-user">
    <el-form :inline="true" :model="queryUserDto" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="queryUserDto.account" placeholder="账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryUserDto.nickname" placeholder="昵称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryUserDto.mobile" placeholder="手机" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryUserDto.roleId" placeholder="角色" clearable>
         <el-option v-for="option in roleList" :key="option.id" :label="option.roleName" :value="option.id"></el-option>
        </el-select>   
      </el-form-item>            
      <el-form-item>
        <el-select v-model="queryUserDto.levelId" placeholder="等级" clearable>
         <el-option v-for="option in levelList" :key="option.id" :label="option.name" :value="option.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isAuth('sys:user:list')" @click="resetFind()" type="info">重置</el-button>
        <el-button v-if="isAuth('sys:user:list')" @click="getDataList()" type = "success">查询</el-button>
        <el-button v-if="isAuth('sys:user:save')"  type="warning " @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:user:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList"  border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column type="index" header-align="center" align="center" width="80" label="序号"></el-table-column>
      <el-table-column prop="account" header-align="center" align="center" label="账号"></el-table-column>
      <el-table-column prop="nickname" header-align="center" align="center" label="昵称"></el-table-column>
      <el-table-column prop="roleId" header-align="center" align="center" label="角色"></el-table-column>
      <el-table-column prop="levelId" header-align="center" align="center" label="商城等级"></el-table-column>
      <el-table-column prop="mobile" header-align="center" align="center" width="160" label="手机号"></el-table-column>
      <el-table-column prop="email" header-align="center" align="center" width="180" label="邮箱"></el-table-column>
      <el-table-column prop="commonStatus" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.commonStatus === 2" size="small" type="danger">封禁</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" width="180" label="创建时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="180" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:user:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('sys:user:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="queryUserDto.pageNum"
      :page-sizes="[10, 20, 50, 100]" :page-size="queryUserDto.pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增/修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './user-add-or-update'
  export default {
    data () {
      return {
        queryUserDto: {
          account: '',
          city: '',
          gender: '',
          levelId: '',
          mobile: '',
          nickname: '',
          pageNum: 1,
          pageSize: 10,
          roleId: '',
          sortColumn: '',
          sortType: true
        },
        dataList: [],
        roleList: [],
        levelList: [],
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getRoleList()
      this.getLevelList()
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/mall-user/user/queryAllUser'),
          method: 'post',
          data: this.queryUserDto
        }).then(({data}) => {
          if (data.code === 200) {
            this.dataList = data.data.records
            this.totalPage = data.data.total
            this.dataList.forEach(item => {
              const role = this.roleList.find(role => role.id === item.roleId)
              const level = this.levelList.find(level => level.id === item.levelId)
              item.roleId = role.roleName
              item.levelId = level.name
            })
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数量变化
      sizeChangeHandle (val) {
        this.queryUserDto.pageSize = val
        this.queryUserDto.pageNum = 1
        this.getDataList()
      },
      // 跳转指定页码
      currentChangeHandle (val) {
        this.queryUserDto.pageNum = val
        this.getDataList()
      },
      // 勾选表格数据
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增/修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        const foundItem = this.dataList.find(item => item.id === id)
        var userNames = id ? [foundItem.nickname] : this.dataListSelections.map(item => {
          return item.nickname
        })
        this.$confirm(`是否对【${userNames.join('、')}】进行${id ? '删除' : '批量删除'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/mall-user/user/deleteUser'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data.code === 200 && data.data === true) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      // 获取全部角色
      getRoleList () {
        this.$http({
          url: this.$http.adornUrl('/admin-login/role/queryAll'),
          method: 'get'
        }).then(({data}) => {
          this.roleList = data.data
        })
      },
      // 获取全部等级
      getLevelList () {
        this.$http({
          url: this.$http.adornUrl('/mall-user/user/level/queryList'),
          method: 'get'
        }).then(({data}) => {
          this.levelList = data.data
        })
      },
      // 重置查询
      resetFind () {
        this.queryUserDto.account = ''
        this.queryUserDto.nickname = ''
        this.queryUserDto.mobile = ''
        this.queryUserDto.roleId = ''
        this.queryUserDto.levelId = ''
        this.getDataList()
      }
    }
  }
</script>
